﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using VOL.Domain.Dto;
using VOL.Domain.IServices;
using VOL.Entity.DomainModels;

namespace VOL.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class EquipmentManagementController : ControllerBase
    {
        private readonly IEquipmentTypeService _equipmentTypeService;
        private readonly IEquipmentledgerService _equipmentledgerService;

        public EquipmentManagementController(IEquipmentTypeService equipmentTypeService,IEquipmentledgerService equipmentledgerService)
        {
            this._equipmentTypeService = equipmentTypeService;
            this._equipmentledgerService = equipmentledgerService;
        }
        /// <summary>
        /// 获取设备类型树形结构
        /// </summary>
        /// <returns>树形结构数据</returns>
        // 确保路由匹配URL
        [HttpGet("/api/EquipmentManagement/GetEquipmentTypeTree")]
        public async Task<ActionResult<List<EquipmentTypeTree>>> GetEquipmentTypeTree()
        {
            var result = await _equipmentTypeService.GetEquipmentTypeTree();
            return Ok(result);
        }
        /// <summary>
        /// 获取设备台账列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<PageGridData<Equipmentledger>> GetEquipmentLedgerPage(GetEquipmentledgerDto dto)
        {
            return _equipmentledgerService.GetEquipmentLedgerPage(dto);
        }

        /// <summary>
        /// 修改设备台账信息
        /// </summary>
        /// <param name="entity">设备台账实体</param>
        /// <returns>操作结果</returns>
        [HttpPost("UpdateEquipmentLedger")]
        public async Task<ActionResult> UpdateEquipmentLedger([FromBody] Equipmentledger entity)
        {
            var result = await _equipmentledgerService.UpdateEquipmentLedger(entity);
            return Ok(result);
        }

        /// <summary>
        /// 删除设备台账信息
        /// </summary>
        /// <param name="id">设备台账ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete("DeleteEquipmentLedger")]
        public async Task<ActionResult> DeleteEquipmentLedger(int id)
        {
            if (id <= 0)
            {
                return BadRequest("参数错误");
            }

            var result = await _equipmentledgerService.DeleteEquipmentLedger(id);
            return Ok(result);
        }
        /// <summary>
        /// 添加设备台账信息
        /// </summary>
        /// <param name="entity">设备台账实体</param>
        /// <returns>操作结果</returns>
        [HttpPost("AddEquipmentLedger")]
        public async Task<ActionResult> AddEquipmentLedger([FromBody] Equipmentledger entity)
        {
            if (entity == null)
            {
                return BadRequest("参数不能为空");
            }
            var result = await _equipmentledgerService.AddEquipmentLedger(entity);
            return Ok(result);
        }
    }
}
